#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QFileDialog>
#include <QDebug>
#include "filereversethread.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),_workdir(""),_counter(0),_preDir("")
{
    ui->setupUi(this);
    qDebug() << "MainWindow Constructor";
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(setWorkDir()));
    qDebug() << "MainWindow connect push button ok";
    _reverseThread = new FileReverseThread;
    connect(_reverseThread, SIGNAL(sendFilePath(QString)), this, SLOT(reveiveFilePath(QString)));
    connect(this, SIGNAL(sendFilePath(QString)), _reverseThread, SLOT(receiveFilePath(QString)));
}

bool isDirExist(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
      return true;
    }
    return false;
}

bool isDirEmpty(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.count() == 3 || dir.count() == 2)
    {
      dir.removeRecursively();
      return true;
    }
    return false;
}

void MainWindow::reveiveFilePath(QString path)
{
    _counter++;
    QString msg = QString::number(_counter);
    msg += ":";
    msg += path;
    if(isDirEmpty(path)){
        QListWidgetItem *dropbox_item = new QListWidgetItem();
        dropbox_item->setIcon(QIcon(":/res/jpg/stylesheet/open.jpg"));
        dropbox_item->setText(path);
        dropbox_item->setSizeHint(QSize(60, 22));
        ui->listWidget->addItem(dropbox_item);
    }
    ui->label_2->setText(msg);
}

void MainWindow::setWorkDir(){
    if(_preDir == "")
        _preDir = QDir::currentPath();
    QString dir = QDir::toNativeSeparators(QFileDialog::getExistingDirectory(this, tr("open path"), _preDir));
    if(!dir.isEmpty()){
        _workdir = dir;
        _preDir  = dir;
    }
    ui->lineEdit->setText(_workdir);
    sendFilePath(_workdir);
    _reverseThread->start();
}

MainWindow::~MainWindow()
{
    delete ui;
}

